<template>
  <div class="permission-form">
    <el-form ref="VerificationVO" :model="VerificationVO" :rules="VerificationVO_Rules" hide-required-asterisk>
      <el-row>
        <el-col :span="18">
          <div class="title-container">
            <h3 class="title">{{ $t('forget.title') }}</h3>
          </div>
        </el-col>
        <el-col :span="6" align="right">
          <el-button type="text" class="back" @click="routerRedirect()">→ {{ $t('forget.back') }}</el-button>
        </el-col>
      </el-row>
      <el-form-item prop="verifiy">
        <span class="icon iconfont verifyCode_icon">&#xe704;</span>
        <el-input v-model="VerificationVO.verifiy" class="verifycode_input" :placeholder="$t('rules.verifyCode')">
          <el-button v-if="isDisabled" slot="append">{{ buttonName }} (s)</el-button>
          <el-button v-else slot="append" @click="getTime">{{ $t('forget.time') }}</el-button>
        </el-input>
      </el-form-item>
      <p class="text">{{ $t('forget.text') }}</p>
      <el-button :loading="loading" type="primary" @click="verification('VerificationVO')">{{ $t('verification.verification') }}</el-button>
    </el-form>
  </div>
</template>
<script>
import { VerificationVO_Rules } from '@/lib/validate/forgetPassword/index.js';
import { VerificationVO } from '@/lib/form/swaggerForm.js';
export default {
  name: 'Verification',
  data () {
    return {
      VerificationVO: new VerificationVO(),
      VerificationVO_Rules: new VerificationVO_Rules(),
      loading: false,
      isDisabled: true,
      buttonName: 60, // 默认显示的秒数
      second: 59 // 倒计时
    };
  },
  created() {
    this.getTime();
  },
  methods: {
    // 倒计时60秒
    getTime() {
      const that = this;
      const interval = window.setInterval(function() {
        that.isDisabled = true;
        that.buttonName = that.second;
        --that.second;
        if (that.second < 0) {
          that.isDisabled = false;
          that.second = 60;
          window.clearInterval(interval);
        }
      }, 1000);
    },
    // 短信验证，成功后显示ResetPassword组件
    verification(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          // 输入的验证码是否正确
          // TODO
          this.loading = true;
          this.$emit('verifiy', false);
        } else {
          return false;
        }
      });
    },
    // 返回登录
    routerRedirect() {
      this.$router.push({
        path: '/login'
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.text {
  color: #ffffff;
  text-align: center;
  font-size: 10px;
}
.permission-container .el-input.verifycode_input {
    width: 100%;
    display: table;
    padding-left: 30px;
    height: 43px;
  .el-button{
    width: 115px;
    height: 43px;
  }
  .el-button.noClick{
    cursor: auto;
  }
}
.verifyCode_icon{
  padding: 1px 5px 5px 14px;
  color: #889aa4;
  vertical-align: middle;
  width: 30px;
  display: inline-block;
  position: absolute;
}
button {
  width: 100%;
}
.back{
  text-align: right;
}
</style>
